package com.guchenbo.schedule.config;

import com.guchenbo.schedule.biz.Demo2QuartzJob;
import com.guchenbo.schedule.biz.DemoQuartzJob;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;

/**
 * @author guchenbo
 * @date 2021/4/25
 */
@Configuration
public class QuartzConfig {

  protected static final Logger log = LoggerFactory.getLogger(QuartzConfig.class);

  @Bean
  public JobDetail jobDetail() {
    JobDetail jobDetail = JobBuilder.newJob().withIdentity("demo").withDescription("demo task")
        .ofType(DemoQuartzJob.class).storeDurably().build();
    return jobDetail;
  }

//  @Bean
  public JobDetailFactoryBean jobDetail2(){
    JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
    factoryBean.setDescription("demo2 task");
    factoryBean.setJobClass(DemoQuartzJob.class);
    factoryBean.setName("demo2");
    factoryBean.setDurability(true);
//    factoryBean.set
    return factoryBean;
  }

  @Bean
  public CronTriggerFactoryBean demoTrigger() {
    CronTriggerFactoryBean factoryBean = new CronTriggerFactoryBean();
    factoryBean.setStartDelay(1000);
    factoryBean.setCronExpression("0 1 1 * * ?");
    factoryBean.setName("cronTrigger");
    factoryBean.setJobDetail(jobDetail());
    return factoryBean;
  }

//  @Bean
  public SimpleTriggerFactoryBean demo2Trigger(){
    SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
    factoryBean.setRepeatInterval(3000);
    factoryBean.setName("simpleTrigger");
    JobDetail jobDetail = JobBuilder.newJob(Demo2QuartzJob.class).withIdentity("demo2")
        .storeDurably(true).build();
    factoryBean.setJobDetail(jobDetail);
    return factoryBean;
  }
}
